using System.Collections.Generic;
using HarmonyLib;
using UnityEngine;

namespace TheWorldOfKongfuEasyMod;

[HarmonyPatch(typeof(PackageController))]
[HarmonyPatch("RefreshPackageUIList")]
public static class Patch_PackageController_RefreshPackageUIList
{
    static void Prefix(ref PackageController __instance,
        ref PackageItemData ___SelectedPackageItem,
        string ___packageTopFilter,
        string ___packageSubFilter,
        string ___tmpTopFilter,
        string ___tmpSubFilter,
        PackageSortType ___packageSortType,
        GameObject ___FilterBtn,
        PackagerFunction ___packagerFunction
    )
    {
        var equipBtnData = __instance.GetComponent<EquipBtnData>();
        if (!equipBtnData)
        {
            Main.Logger.LogError("can't find EquipBtnData");
            return;
        }

        if(___packagerFunction== PackagerFunction.PlayerPackage)
            return;
        Main.Logger.LogInfo(equipBtnData.currentSlotName);
        if (equipBtnData.currentSlotName != "0")
        {
            string Id = null;
            if (___packagerFunction == PackagerFunction.Equip)
            {
                var row = CommonResourcesData.b07.Find_ID(equipBtnData.currentSlotName);
                Id = row.ID;
                
                Main.Logger.LogInfo(JsonUtility.ToJson(row));
            }
            else  if (___packagerFunction == PackagerFunction.Wugong)
            {
                var row = CommonResourcesData.b03.Find_ID(equipBtnData.currentSlotName);
                Id = row.ID;
                
                Main.Logger.LogInfo(JsonUtility.ToJson(row));
            }
            
            if (Id == null)
            {
                Main.Logger.LogError($"can't find row with id is {equipBtnData.currentSlotName}");
                return;
            }

//            Main.Logger.LogInfo("======================");
//            Main.Logger.LogInfo(SharedData.Instance().OpenPackageFor);
            List<PackageItemData> list = PackageController.GetPackageList(___packagerFunction, ___packageSortType);
//            foreach (var itemData in list)
//            {
//                Main.Logger.LogInfo($"[{JsonUtility.ToJson(itemData.b07Row)}]");
//            }
            list.RemoveAll((PackageItemData t) => !PackageController.CouldPassUseFilter(t.b07Row,
                ___tmpTopFilter, ___tmpSubFilter, ___FilterBtn.activeInHierarchy, ___packageTopFilter, ___packageSubFilter));
            list.RemoveAll((PackageItemData t) => t.b07Row.Name_Trans.StartsWith("★ "));
//            Main.Logger.LogInfo("+++++++++++++");
//            foreach (var itemData in list)
//            {
//                Main.Logger.LogInfo($"[{JsonUtility.ToJson(itemData.b07Row)}]");
//            }
//            Main.Logger.LogInfo("======================");
            
            var packageItemData = list.Find(x => ___packagerFunction == PackagerFunction.Wugong? (x.b07Row.Relateid == Id): (x.ID == Id));

            if (packageItemData == null)
            {
                Main.Logger.LogError($"can't find packageItemData with id is {Id}");
                return;
            }

            ___SelectedPackageItem = packageItemData;
        }
    }
}